﻿<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0
        }
        .top, #Q-nav1{
            text-align: center;
        }
        img {
            vertical-align: top;
        }

        .main {
            margin: 0 auto;
            width: 1000px;
            margin-top: 10px;

        }

        #Q-nav1 {
            overflow: hidden;
        }

        .fixed {
            position: fixed;
            top: 0;
            left: 0;
        }
    </style>

</head>
<body>
    <div class="top" id="top">
        <img src="images/top.png" alt="" class="top-img"/>
    </div>
    <div id="Q-nav1">
        <img src="images/nav.png" alt=""/>
    </div>
    <div class="main" id="main">
        <img src="images/main.png" alt=""/>
    </div>

    <!--引入工具js-->
    <script src="my.js"></script>
    <script>
        window.onload = function () {
            var topDiv = document.getElementById('top');
            var height = topDiv.offsetHeight;
            var middle = document.getElementById('Q-nav1');
            var main = document.getElementById('main');

            window.onscroll = function () {
                if(scroll().top >height){
                    middle.className = 'fixed';
                    //第二个盒子不占位置了，所以我们给第三个盒子一个上padding占位置，不出现盒子抖动问题
                    main.style.paddingTop = middle.offsetHeight+"px";
                }else{
                    middle.className = '';
                    //清零
                    main.style.paddingTop = 0;
                }
            }

        }
    </script>
</body>
</html>
